﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using Entity;
using DAL;
using Models;
using System.Linq.Expressions;
namespace BAL
{
    public class ExamRoom : BaseBAL<ExamRoomInfo, Models.ExamRoom>
    {
        private static string CacheName = "AllExamRoom";

        /// <summary>
        /// 写入缓存
        /// </summary>
        /// <returns></returns>
        public static void ToCache()
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                CacheHelper.Refurbish(CacheName, db.ExamRooms.ToList(), 0, 60);
            }
        }

        /// <summary>
        /// 取得一条
        /// </summary>
        /// <param name="InputId">信息ID</param>
        /// <returns></returns>
        public static ExamRoomInfo GetSingleInfo(int InputId)
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.ExamRoom> examRoomInfo = (IList<Models.ExamRoom>)CacheHelper.GetCache(CacheName);
            return TransT2ToT1(examRoomInfo.Single(b => b.Id == InputId));
        }

        /// <summary>
        /// 取得全部列表
        /// </summary>
        /// <returns></returns>
        public static IList<ExamRoomInfo> GetList()
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.ExamRoom> examRoomInfo = (IList<Models.ExamRoom>)CacheHelper.GetCache(CacheName);
            return examRoomInfo.Select(t => TransT2ToT1(t)).ToList();
        }
        private static Expression<Func<Models.ExamRoom, bool>> GetExamRoomExpression(string name, DateTime? startDateTime, DateTime? endDateTime)
        {
            var predicate = PredicateBuilder.True<Models.ExamRoom>();
            if (!PageValidate.IsEmpty(name))
                predicate = predicate.And(p => p.ExamName.Contains(name));
            if (startDateTime != null)
                predicate = predicate.And(p => p.ExamStartDate >= startDateTime);
            if (endDateTime != null)
                predicate = predicate.And(p => p.ExamEndDate <= endDateTime);
            return predicate;
        }
        public static int GetCount(string name,DateTime? startDateTime,DateTime? endDateTime)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                return db.GetTable<Models.ExamRoom>().Where(GetExamRoomExpression(name, startDateTime, endDateTime)).Count();
            }
        }

        public static IList<ExamRoomInfo> GetList(string name, DateTime? startDateTime, DateTime? endDateTime, int first, int max)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                if (max > 0)
                {
                    return db.GetTable<Models.ExamRoom>().Where(GetExamRoomExpression(name, startDateTime, endDateTime)).Skip(first).Take(max).Select(t => TransT2ToT1(t)).ToList();
                }
                else
                {
                    return db.GetTable<Models.ExamRoom>().Where(GetExamRoomExpression(name, startDateTime, endDateTime)).Select(t => TransT2ToT1(t)).ToList();
                }
            }
        }
        /// <summary>
        /// 保存信息
        /// </summary>
        /// <param name="InstInfo">如果id=0则新增>0时编辑</param>
        /// <returns></returns>
        public static ExamRoomInfo Save(ExamRoomInfo InstInfo)
        {
            ExamRoomInfo examRoomInfo = InstInfo;
            if (InstInfo.Id > 0)
            {
                Edit(InstInfo);
            }
            else
            {
                examRoomInfo = Add(InstInfo);
            }
            ToCache();
            return examRoomInfo;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">id</param>
        public static void DelInfo(int id)
        {
            StudentScore.DelByExamRoomId(id);
            Delete(id);
            ToCache();
        }
    }
}
